package com.ccit.greenlife.service.impl;

import com.ccit.greenlife.Mapper.ProductMapper;
import com.ccit.greenlife.pojo.PageBean;
import com.ccit.greenlife.pojo.Product;
import com.ccit.greenlife.service.ProductService;
import com.ccit.greenlife.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static java.time.LocalDateTime.now;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    //分页查询
    @Override
    public PageBean getAllProducts(Integer page, Integer pageSize, String type, String name) {
        //1.设置分页查询参数（页码，每页显示数量）
        PageHelper.startPage(page, pageSize);
        //2.查询所有数据，强转为page类型
        Page p = (Page) productMapper.getAllProducts(type, name);
        // 3. 封装结果到 PageBean(当前页的数据 总记录数 总页数 当前页码 每页大小)
        PageBean<Product> pageBean = new PageBean<>(p, p.getTotal(), p.getPages(), p.getPageNum(), p.getPageSize());
        return pageBean;
    }
}
